package com.hm.ibsm.web.mg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hm.common.util.CommonUtil;
import com.hm.ibsm.auto.helper.SystemSessionHolder;
import com.hm.ibsm.service.MenuService;
import com.hm.ibsm.util.BaseController;
import com.hm.ibsm.vo.auth.UserMenuVo;
import com.hm.ibsm.vo.menu.MenuVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年3月24日 下午6:09:51
 * @version 1.0
 * @describe
 */
@Slf4j
@Controller
@RequestMapping("/mg/menu")
public class MenuController extends BaseController {

	private String MODEL_NAME = MenuController.class.getName();

	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/index.do", method = RequestMethod.GET)
	public ModelAndView menuMg() {
		try {
			Map<String, Object> mapper = new HashMap<String, Object>();
			{
				mapper.put("menuTree", loopMenus(menuService.loadMenus(), new JSONArray(), "0", true).toJSONString());
			}

			return new ModelAndView("mg/menu", mapper);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/add.do", method = RequestMethod.GET)
	public ModelAndView addMenu(@RequestParam(name = "parentId", required = false) String parentId) {
		try {
			Map<String, Object> mapper = new HashMap<String, Object>();
			{
				mapper.put("parentId", parentId);
			}

			return new ModelAndView("mg/menu/addMenu", mapper);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/update.do", method = RequestMethod.GET)
	public ModelAndView updateMenu(@RequestParam(name = "id", required = true) String id) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("result", menuService.getMenuById(id));
			return new ModelAndView("mg/menu/updateMenu", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/detail.do", method = RequestMethod.GET)
	public ModelAndView detailMenu(@RequestParam(name = "id", required = true) String id) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("result", menuService.getMenuById(id));
			return new ModelAndView("mg/menu/detailMenu", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/addMenu.do", method = RequestMethod.POST)
	public ModelAndView saveAddMenu(MenuVo menu) {
		try {
			menuService.addMenu(menu);
			return menuMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/updateMenu.do", method = RequestMethod.POST)
	public ModelAndView saveUpdateMenu(MenuVo menu) {
		try {
			menuService.updateMenu(menu.getId(), menu);
			return menuMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/deleteMenu.do", method = RequestMethod.GET)
	public ModelAndView saveDeleteMenu(@RequestParam(name = "id", required = true) String id) {
		try {
			menuService.deleteMenu(id);
			return menuMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/query.do", method = RequestMethod.GET)
	public @ResponseBody String query(MenuVo menu) {
		return json(menuService.query(SystemSessionHolder.pageParam(), CommonUtil.of(menu.getId()))).toString();
	}

	private JSONArray loopMenus(List<UserMenuVo> menus, JSONArray items, String parentId, boolean spread) {
		for (UserMenuVo menu : menus) {
			JSONObject item = new JSONObject();
			{
				item.put("id", menu.getMenuId());
				item.put("name", menu.getMenuName());
				if (spread) {
					item.put("spread", spread);
					spread = false;
				}
			}
			if (menu.isHasChild()) {
				item.put("children", loopMenus(menu.getChilds(), new JSONArray(), menu.getMenuId(), false));
			}
			items.add(item);
		}
		return items;
	}
}
